package cn.stylefeng.guns.modular.web.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.web.entity.Gift;
import cn.stylefeng.guns.modular.web.service.IGiftService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 描述：礼品表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-26
 */
@Controller
@RequestMapping("/web/gift")
public class GiftController extends BaseController {

    private String PREFIX = "/modular/web/gift/";

    @Autowired
    private IGiftService giftService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "gift.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = giftService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/修改页面
     * @param giftId
     * @param model
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String giftId, Model model) {
        model.addAttribute("giftId", giftId);
        return PREFIX + "gift_add.html";
    }

    /**
     * 保存
     * @param gift
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public ResponseData save(Gift gift) {
        if (ToolUtil.isEmpty(gift.getId())) {
            gift.preInsert();
            giftService.save(gift);
        }else{
            gift.preUpdate();
            giftService.updateById(gift);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param giftId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{giftId}")
    public Object getInfoById(@PathVariable("giftId") String giftId){
        Map<String, Object> res = giftService.getInfoById(giftId);
        return res;
    }

    /**
     * 删除
     * @param giftId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseData delete(@RequestParam(name = "giftId") String giftId){
        if(ToolUtil.isEmpty(giftId)){
            throw new ServiceException(BizExceptionEnum.PARAM_ERROR);
        }
        Gift gift = giftService.getById(giftId);
        gift.preUpdate();
        gift.setDelFlag(1);
        giftService.updateById(gift);
        return SUCCESS_TIP;
    }

}
